package control

import (
	"mvip-route/beans"
	"mvip-route/service/damRed/impl"
	"mvip-route/service/model"
	"strconv"

	"github.com/gin-gonic/gin"
)

type DameRedContr struct {
	DameRedService impl.DamRedCrudService
}

func (ctrl *DameRedContr) CreateDameRed(c *gin.Context) {
	var newDameRed model.DamRedVo
	if err := c.ShouldBindJSON(&newDameRed); err != nil {
		beans.Fail(c, err.Error())
		return
	}
	err := ctrl.DameRedService.CreateDameRed(newDameRed)
	if err != nil {
		beans.Fail(c, err.Error())
		return
	}
	beans.Success(c, true)
}

func (ctrl *DameRedContr) GetDameRed(c *gin.Context) {
	idStr := c.Query("id")
	if idStr == "" {
		beans.Fail(c, "请选择域名")
		return
	}
	id, err := strconv.Atoi(idStr)
	if err != nil {
		// 处理转换错误
		beans.Fail(c, "Invalid ID format")
		return
	}

	service := impl.DamRedService{}
	red, err := service.GetDameRed(id)
	if err != nil {
		beans.Fail(c, err.Error())
		return
	}
	beans.Success(c, red)
}

func (ctrl *DameRedContr) GetDameReds(c *gin.Context) {
	dameReds, err := ctrl.DameRedService.GetDameReds()
	if err != nil {
		beans.Fail(c, "查询失败")
		return
	}
	beans.Success(c, dameReds)
}
func (ctrl *DameRedContr) UpdateDameRed(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var updatedDameRed model.DamRedVo
	if err := c.ShouldBindJSON(&updatedDameRed); err != nil {
		beans.Fail(c, "请选择")
		return
	}
	service := impl.DamRedCrudService{}
	err := service.UpdateDameRed(updatedDameRed, id)
	if err != nil {
		beans.Fail(c, err.Error())
		return
	}
	beans.Success(c, true)
}
func (ctrl *DameRedContr) DeleteDameRed(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	service := impl.DamRedCrudService{}
	if err := service.DeleteDameRed(id); err != nil {
		beans.Fail(c, err.Error())
		return
	}
	beans.Success(c, true)
}
